`
https://leetcode.cn/problems/longest-zigzag-path-in-a-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var longestZigZag = function (root) {
  let res = 0

  // 返回一个数组
  // [0] 表示向左走的最长交错路径长度
  // [1] 表示向右走的最长交错路径长度
  const getMaxCrossLen = (root) => {
    if (root === null) return [-1, -1]

    const left = getMaxCrossLen(root.left)
    const right = getMaxCrossLen(root.right)

    const rootPathLen1 = left[1] + 1
    const rootPathLen2 = right[0] + 1

    res = Math.max(res, rootPathLen1, rootPathLen2)

    return [rootPathLen1, rootPathLen2]
  }

  getMaxCrossLen(root)

  return res
};